package com.micro.turtle.runner;

import cn.hutool.core.collection.CollectionUtil;
import com.micro.turtle.anon.CmdRunner;
import com.micro.turtle.constant.Cmd;
import com.micro.turtle.constant.Url;
import com.micro.turtle.constant.Url.JobUri;
import com.micro.turtle.model.Job;
import com.micro.turtle.model.resp.JobResp;
import com.micro.turtle.util.Api;
import com.micro.turtle.util.Log;
import java.util.List;

/**
 * JobRunner
 *
 * @author jason.lv
 * @since 2022/1/20 16:23
 */
public class JobRunner {

    @CmdRunner
    public static class ListRunner extends Runner {

        public ListRunner() {
            super(Cmd.JOBS);
        }

        @Override
        void exec() {
            String projectId = scannerNext("输入 Project Id:");
            String pipelineId = scannerNext("输入 Pipeline Id:");
            print(list(Integer.valueOf(projectId), Integer.valueOf(pipelineId)));
        }
    }

    @CmdRunner
    public static class DetailRunner extends Runner {

        public DetailRunner() {
            super(Cmd.JOB_DETAIL);
        }

        @Override
        void exec() {
            String projectId = scannerNext("输入 Project Id:");
            String pipelineId = scannerNext("输入 Pipeline Id:");
            print(list(Integer.valueOf(projectId), Integer.valueOf(pipelineId)));
        }
    }

    @CmdRunner
    public static class PlayRunner extends Runner {

        public PlayRunner() {
            super(Cmd.JOB_PLAY);
        }

        @Override
        void exec() {
            String projectId = scannerNext("输入 Project Id:");
            String jobId = scannerNext("输入 Job Id:");
            JobResp job = play(Integer.valueOf(projectId), Integer.valueOf(jobId));
            Log.info("Job Play >>>>> Id:%s ||| %s ||| %s ||| %s", job.getId(), job.getName(), job.getStatus(), job.getRef());
        }
    }


    public static void print(List<Job> jobs) {
        if (CollectionUtil.isEmpty(jobs)) {
            Log.info("Jobs >>>>> has nothing");
            return;
        }
        for (Job job : jobs) {
            Log.info("Job >>>>> Id:%s ||| %s ||| %s ||| %s", job.getId(), job.getName(), job.getStatus(), job.getRef());
        }
    }

    public static List<Job> list(Integer projectId, Integer pipelineId) {
        Url url = Url.create(JobUri.LIST).project(projectId).pipeline(pipelineId);
        return Api.getList(url, Job.class);
    }

    public static Job detail(Integer projectId, Integer jobId) {
        Url url = Url.create(JobUri.DETAIL).project(projectId).job(jobId);
        return Api.get(url, Job.class);
    }


    public static JobResp play(Integer projectId, Integer jobId) {
        Url url = Url.create(JobUri.PLAY).project(projectId).job(jobId);
        return Api.post(url, JobResp.class);
    }

    public static JobResp retry(Integer projectId, Integer jobId) {
        Url url = Url.create(JobUri.RETRY).project(projectId).job(jobId);
        return Api.post(url, JobResp.class);
    }

    public static JobResp cancel(Integer projectId, Integer jobId) {
        Url url = Url.create(JobUri.CANCEL).project(projectId).job(jobId);
        return Api.post(url, JobResp.class);
    }

    public static JobResp delete(Integer projectId, Integer jobId) {
        Url url = Url.create(JobUri.DELETE).project(projectId).job(jobId);
        return Api.delete(url, JobResp.class);
    }
}
